package com.hshoesmall.demo.controller;

import com.hshoesmall.demo.common.api.ApiResult;
import com.hshoesmall.demo.mapper.ShoesByPageMapper;
import com.hshoesmall.demo.model.entity.ShoesByPage;
import com.hshoesmall.demo.vo.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

@RequestMapping("/shoes")
public class ShoesByPageController {

    @Resource
    ShoesByPageMapper shoesByPageMapper;
    @RequestMapping(value="/getShoesByPage", method = RequestMethod.POST)

//    public Page<ShoesByPage> findByPage(@RequestParam(defaultValue = "1") Integer pageNo,
//                                        @RequestParam(defaultValue = "10") Integer pageSize
//                                        ) {
//
//        Integer offset = (pageNo - 1) * pageSize;
//        List<ShoesByPage> shoesData = shoesByPageMapper.findShoesListByPage(offset,pageSize);
////        List<ShoesByPage> shoesData = shoesByPageService.findShoesByPage(offset,pageSize);
//        Page<ShoesByPage> page = new Page<>();
//        page.setData(shoesData);
//
//        Integer total = shoesByPageMapper.countShoes();
//        page.setTotal(total);
//        page.setPageNo(pageNo);
//        page.setPageSize(pageSize);
//
//        return page;
//
//    }

    public ApiResult<Map<String, Object>> findByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                                     @RequestParam(defaultValue = "",required = false) Integer[] sbids,
                                                     @RequestParam(defaultValue = "",required = false) Integer[] stids,
                                                     @RequestParam(defaultValue = "",required = false) Integer[] sizeids,
                                                     @RequestParam(defaultValue = "",required = false) Integer[] scids,
                                                     @RequestParam(defaultValue = "",required = false) String[] sgenders
                                        ) {
        Integer offset = (pageNo - 1) * pageSize;

        List<ShoesByPage> shoesData = shoesByPageMapper.findShoesListByPage(offset,pageSize,sbids,stids,sizeids,scids,sgenders);
        if(ObjectUtils.isEmpty(shoesData)){
            return ApiResult.failed("操作失败");

        }
        Page<ShoesByPage> page = new Page<>();
        page.setData(shoesData);

        Integer total = shoesByPageMapper.countShoes();
        page.setTotal(total);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Map<String, Object> map = new HashMap<>(16);
        map.put("shoes", page);
        return ApiResult.success(map);
//        return page;


    }

}
